package com.zr.bookcorner.fragment;

import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zh.baselibrary.base.BaseFragment;
import com.zh.baselibrary.base.BasePresenter;
import com.zh.baselibrary.constant.BaseConstant;
import com.zh.baselibrary.model.BookModel;
import com.zh.baselibrary.netutils.BaseResp;
import com.zh.baselibrary.rxjava.RxSubscribe;
import com.zh.baselibrary.utils.ComparatorUtils;
import com.zh.baselibrary.utils.KeyBoardUtils;
import com.zh.baselibrary.utils.PhoneUtil;
import com.zh.baselibrary.utils.ShareManager;
import com.zh.baselibrary.views.SearchView;
import com.zh.model.manager.BookInfo;
import com.zr.bookcorner.BookApplication;
import com.zr.bookcorner.R;
import com.zr.bookcorner.adapter.BookSelectAdapter;
import com.zr.bookcorner.constant.Constant;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.OnEditorAction;
import butterknife.OnFocusChange;
import butterknife.OnTextChanged;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class BookSelectFragment extends BaseFragment {
    @BindView(R.id.rv_book)
    RecyclerView mRvBook;
    @BindView(R.id.sv_text)
    SearchView mSvText;

    private Boolean isFocus = false;
    private BookModel model;
    private  BookSelectAdapter mAdapter;
    String schoolId;
    String uuid;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_book_select;
    }

    @Override
    protected void init() {
         mAdapter = new BookSelectAdapter(new ArrayList<>());
        RecyclerView.LayoutManager manager;
        if(BookApplication.isBig())
            manager = new GridLayoutManager(getActivity(), 2);
        else
            manager = new LinearLayoutManager(getActivity());
        mRvBook.setLayoutManager(manager);
        mRvBook.setAdapter(mAdapter);
        model = new BookModel(getContext());
        schoolId = ShareManager.getValue(getContext(), BaseConstant.DEVICE_SCHOOL_ID);
        uuid = PhoneUtil.getUUID(getContext());
        queryBook("");

    }
    //键盘反馈
    @OnEditorAction(R.id.sv_text)
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            KeyBoardUtils.closeKeybord(mSvText, getActivity());
            queryBook(mSvText.getText().toString());
        }
        return false;
    }
    @OnTextChanged({R.id.sv_text})
    public void onTextChanged(CharSequence text, int start, int before, int count) {
        queryBook(mSvText.getText().toString());
    }
    @OnFocusChange({R.id.sv_text})
    public void onFocusChange(View v, boolean hasFocus) {
        isFocus = hasFocus;
    }
    /**
     * 关闭软键盘
     */
    public void hideSoftInput() {
        if (mSvText != null && isFocus) {
            KeyBoardUtils.closeKeybord(mSvText,getContext());
        }
    }
    private void queryBook(String content) {
            model.selectBook(schoolId,uuid,content)
                 .subscribeOn(Schedulers.io())//启动订阅线程
                .observeOn(AndroidSchedulers.mainThread())//切换至主线程
                    .subscribe(new RxSubscribe<BaseResp<List<BookInfo>>>() {
                @Override
                public void onSuccess(BaseResp<List<BookInfo>> listBaseResp) throws Exception {
                    if (listBaseResp.isSuccess()) {
                        mAdapter.clearAdd(listBaseResp.getData());
                    }
                }

                @Override
                public void onHint(String hint) {

                }
            });
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    public void onDestroyView() {
        hideSoftInput();
        super.onDestroyView();

    }
}
